package com.itheima.api.bigdecimal;

import java.math.BigDecimal;

public class BigDecimal1Damo {
    //如果想要进行精确运算,那么需要使用字符串构造
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.1");
        double a = 0.1;
        System.out.println("bd1 = " + bd1);

        BigDecimal bd2 = new BigDecimal("0.2");
        System.out.println("bd2 = " + bd2);
        double b = 0.2;

        BigDecimal add = bd1.add(bd2);
        System.out.println("add = " + add);
        System.out.println("a+b = " + (a + b));


        BigDecimal subtract = bd1.subtract(bd2);
        System.out.println("subtract = " + subtract);
        System.out.println("a-b = " + (a - b));

        BigDecimal multiply = bd1.multiply(bd2);
        System.out.println("multiply = " + multiply);
        System.out.println("(a*b) = " + (a * b));

        BigDecimal divide = bd1.divide(bd2);
        System.out.println("divide = " + divide);
        System.out.println("(a/b) = " + (a / b));

        BigDecimal bd3 = new BigDecimal("12.1");
        BigDecimal bd4 = new BigDecimal("1.21");
        BigDecimal divide1 = bd3.divide(bd4);
        System.out.println("divide1 = " + divide1);

        BigDecimal bd5 = new BigDecimal("10.0");
        BigDecimal bd6 = new BigDecimal("3.0");
        //divide(double 运算数,int 小数位数, BigDecimal.ROUND_HALF_UP)
        //进一法:    BigDecimal.ROUND_UP
        //去尾法:    BigDecimal.ROUND_FLOOR
        //四舍五入:  BigDecimal.ROUND_HALF_UP
        System.out.println("bd5.divide(bd6,BigDecimal.ROUND_UP) = " + bd5.divide(bd6,2, BigDecimal.ROUND_UP));
        System.out.println("bd5.divide(bd6,2,BigDecimal.ROUND_FLOOR) = " + bd5.divide(bd6, 2, BigDecimal.ROUND_FLOOR));
        System.out.println("bd5.divide(bd6,2,BigDecimal.ROUND_HALF_UP) = " + bd5.divide(bd6, 2, BigDecimal.ROUND_HALF_UP));
        double doubleValue = bd5.doubleValue();
        System.out.println("doubleValue = " + doubleValue);
    }
}
